/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dusin.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class DateUtil {

    public static Date get(int year, int month, int day, int hour, int min, int second) {
        Date rtDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        try {
            rtDate = sdf.parse("" + year + "-" + month + "-" + day + "-" + hour + "-" + min + "-" + second);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return rtDate;
    }

    public static String get(Date time, String pattern) {
        String rtString = null;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        rtString = sdf.format(time);
        return rtString;
    }

    //返回当前时间字符串
    public static String get() {
        return get(new Date());
    }

    public static String get(Date time) {
        String rtString = null;
        if (time != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            rtString = sdf.format(time);
        }
        return rtString;
    }

    public static Date get(String time, String pattern) {
        Date rtDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            rtDate = sdf.parse(time);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return rtDate;
    }

    public static String changePattern(String dateStr, String patternFrom, String patternTo) {
        Date date = get(dateStr, patternFrom);
        return get(date, patternTo);
    }

    public static Date get(String time) {
        return get(time, "yyyy-MM-dd HH:mm:ss");
    }

    public static String getString(int year, int month, int day, int hour, int min, int second) {
        return "" + year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + second;
    }

    /**
     * 将整数日期转换成字符串日期，�? 201603->"2016-03",20160402->"2016-04-02"
     *
     * @param date 整数表示的时间，必须保证格式正确，不做格式检查
     * @param s 分隔字符
     * @return
     */
    public static String toDate(int date, char s) {
        if (date > 1000000) {//6位表示的年月�?
            int year = date / 10000;
            int month = (date % 10000) / 100;
            int day = date % 100;
            return "" + year + s + n2String(month) + s + n2String(day);
        } else {//4位日期整数，表示年月
            int year = date / 100;
            int month = date % 100;
            return "" + year + s + n2String(month);
        }

    }

    /**
     * 将数字转换成2位字符串，不足两位前面补零�?�如4->"04"
     *
     * @param d
     * @return
     */
    public static String n2String(int d) {
        if (d < 10) {
            return "0" + d;
        } else {
            return "" + d;
        }
    }

    static boolean isLeap(int year) {
        if (((year % 100 == 0) && year % 400 == 0) || ((year % 100 != 0) && year % 4 == 0)) {
            return true;
        } else {
            return false;
        }
    }

    public static int getDays(int year, int month) {
        int days;
        int FebDay = 28;
        if (isLeap(year)) {
            FebDay = 29;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 2:
                days = FebDay;
                break;
            default:
                days = 0;
                break;
        }
        return days;
    }

    /**
     *
     * @param year
     * @param month
     * @return 返回上个月�?�如参数�?2017,7则返�?"201706"，如果是2017,1，则返回"201612"
     */
    public static String previousMonth(int year, int month) {
        int rMonth;
        int rYear;
        if (month > 1) {
            rMonth = month - 1;
            rYear = year;
        } else {
            rMonth = 12;
            rYear = year - 1;
        }
        if (rMonth < 10) {
            return rYear + "0" + rMonth;
        } else {
            return rYear + "" + rMonth;
        }
    }

    public static String previousMonth(String yearMonth) {
        int year = Integer.parseInt(yearMonth.substring(0, 4));
        int month = Integer.parseInt(yearMonth.substring(4));
        return previousMonth(year, month);
    }

    public static String getYestoday(Calendar now) {
        Calendar yestoday = (Calendar) now.clone();
        yestoday.add(Calendar.DAY_OF_MONTH, -1);
        return DateUtil.get(yestoday.getTime(), "yyyyMMdd");
    }

    //返回一个年月的列表
    public static List<String> getYearMonthList(String startYearMonth, String endYearMonth) {
        List<String> yearMonthList = new ArrayList<String>();
        Calendar calStart = Calendar.getInstance();
        calStart.set(Calendar.YEAR, Integer.parseInt(startYearMonth.substring(0, 4)));
        calStart.set(Calendar.MONTH, Integer.parseInt(startYearMonth.substring(4))-1);
        Calendar calEnd = Calendar.getInstance();
        calEnd.set(Calendar.YEAR, Integer.parseInt(endYearMonth.substring(0, 4)));
        calEnd.set(Calendar.MONTH, Integer.parseInt(endYearMonth.substring(4))-1);
        calEnd.add(Calendar.DAY_OF_MONTH, 1);
        while (calStart.before(calEnd)) {
            String ym = DateUtil.get(calStart.getTime(), "yyyyMM");
            yearMonthList.add(ym);
            calStart.add(Calendar.MONTH, 1);
        }
        return yearMonthList;
    }
}
